<?php
include_once('./config/config.php');
// 接收参数id
$id = $_GET['id'];
// 通过id获取信息
$sql = "select * from job where id = '$id'";
$sql = "SELECT j.name, j.id, d.name as dname from job as j LEFT JOIN department as d ON j.depid = d.id where j.id = '$id'";
$admin = findOne($sql);

if ($_POST) {
  // 接收全部参数
  // var_dump($_POST);
  $data = [
    "name" => $_POST['jobname'],
    "depid" => $_POST['depname']
  ];

  $i = update("job", $data, "id = '$id'");
  if ($i) {
    ShowMsg("更新职位成功", 'jobList.php');
  } else {
    ShowMsg("更新职位失败");
  }
}

// 获取所有部门信息
$depRes = findAll("select * from department");
if (!$depRes) {
  ShowMsg("dep数据正在紧急修复中");
  exit;
} 
?>

<!DOCTYPE html>
<html lang="en">

<head>
  <!-- 引入 -->
  <?php include_once('./common/meta.php') ?>
</head>

<body>
  <!-- 引入头部导航 -->
  <?php include_once('./common/navbar.php') ?>
  <!-- 引入侧边菜单栏 -->
  <?php include_once('./common/sidebar.php') ?>

  <div class="content">
    <div class="header">
      <h1 class="page-title">编辑职位</h1>
    </div>
    <ul class="breadcrumb">
      <li><a href="index.php">Home</a> <span class="divider">/</span></li>
      <li class="active">editJob</li>
    </ul>

    <div class="container-fluid">
      <div class="row-fluid">
        <div class="btn-toolbar">
          <button class="btn btn-primary" onClick="location='jobList.php'"><i class="icon-list"></i> 返回列表</button>
          <div class="btn-group">
          </div>
        </div>

        <div class="well">
          <div id="myTabContent" class="tab-content">
            <div class="tab-pane active in" id="home">
              <form method="POST" enctype="multipart/form-data">

                <!-- 用户名 -->
                <label>职位名称</label>
                <input type="text" class="input-xxlarge" name="jobname" id="depname" value="<?php echo $admin['name'] ?>" required>
                <label>所属部门</label>
                <select name="depname" id="dep">
                  <?php foreach($depRes as $item){ ?>
                    <option value="<?php echo $item['id'] ?>"> <?php echo $item['name'] ?> </option>
                  <?php } ?>
                </select>
                <br>
                <button class="btn btn-primary" id="subBtn" value="提交">提交</button>
              </form>
            </div>
          </div>
        </div>

        <!-- 引入底部 -->
        <?php include_once('./common/footer.php') ?>

      </div>
    </div>
  </div>

</body>

</html>
<!-- 引用公共js文件 -->
<?php include_once('./common/script.php') ?>

<script>
  $(document).ready(function() {
    
  })
</script>
